#pragma once
#include <windows.h>

//JLINK TIF
#define JLINK_TIF_JTAG          0
#define JLINK_TIF_SWD           1
 
//RESET TYPE
#define JLINK_RESET_TYPE_NORMAL 0
#define JLINK_RESET_TYPE_CORE   1
#define JLINK_RESET_TYPE_PIN    2

typedef struct {
	DWORD                   SizeOfStruct;           // Required. Use SizeofStruct = sizeof(JLINKARM_DEVICE_INFO)
	const char*             sName;
	DWORD                   CoreId;
	DWORD                   FlashAddr;              // Start address of first flash area
	DWORD                   RAMAddr;                // Start address of first RAM area
	char                    EndianMode;             // 0=Little, 1=Big, 2=Both
	DWORD                   FlashSize;              // Total flash size in bytes (flash may contain gaps. For exact address & size of each region, please refer to aFlashArea)
	DWORD                   RAMSize;                // Total RAM size in bytes  (RAM may contain gaps. For exact address & size of each region, please refer to aRAMArea)
	const char*             sManu;                  // Device manufacturer
	char                    ignore[1024];
} JLINKARM_DEVICE_INFO;

typedef struct {
	DWORD SizeOfStruct;                             // Required. Use SizeofStruct = sizeof(JLINKARM_DEVICE_SELECT_INFO)
	DWORD CoreIndex;
} JLINKARM_DEVICE_SELECT_INFO;

extern void  (WINAPI *JLINK_SetLogFile)(char *file);
extern DWORD (WINAPI *JLINK_GetDLLVersion)(void);
extern DWORD (WINAPI *JLINK_GetHardwareVersion)(void);
extern DWORD (WINAPI *JLINK_GetFirmwareString)(char *buff, int count);
extern DWORD (WINAPI *JLINK_GetSN)(void);
extern BOOL  (WINAPI *JLINK_ExecCommand)(char* cmd, int a, int b);
extern DWORD (WINAPI *JLINK_TIF_Select)(int type);
extern void  (WINAPI *JLINK_SetSpeed)(int speed);
extern DWORD (WINAPI *JLINK_GetSpeed)(void);
extern DWORD (WINAPI *JLINK_GetId)(void);

extern int   (       *JLINK_DEVICE_GetIndex)(const char* sDeviceName);
extern int   (       *JLINK_DEVICE_GetInfo)(int DeviceIndex, JLINKARM_DEVICE_INFO* pDeviceInfo);
extern int   (       *JLINK_DEVICE_SelectDialog)(void* hParent, DWORD Flags, JLINKARM_DEVICE_SELECT_INFO* pInfo);

extern BOOL  (WINAPI *JLINK_Open)(void);
extern void  (WINAPI *JLINK_Close)(void);
extern BOOL  (WINAPI *JLINK_IsOpen)(void);
extern BOOL  (WINAPI *JLINK_Connect)(void);
extern BOOL  (WINAPI *JLINK_IsConnected)(void);
extern int   (WINAPI *JLINK_Halt)(void);
extern BOOL  (WINAPI *JLINK_IsHalted)(void);
extern void  (WINAPI *JLINK_SetResetType)(int type);
extern void  (WINAPI *JLINK_Reset)(void);
extern void  (WINAPI *JLINK_Go)(void);
extern void  (WINAPI *JLINK_GoIntDis)(void);
extern DWORD (WINAPI *JLINK_ReadReg)(int index);
extern int   (WINAPI *JLINK_WriteReg)(int index, DWORD data);

extern int   (WINAPI *JLINK_ReadMem)(DWORD addr, int len, void *buf);
extern int   (WINAPI *JLINK_WriteMem)(DWORD addr, int len, void *buf);
extern int   (WINAPI *JLINK_WriteU8)(DWORD addr, BYTE data);
extern int   (WINAPI *JLINK_WriteU16)(DWORD addr, WORD data);
extern int   (WINAPI *JLINK_WriteU32)(DWORD addr, DWORD data);

extern int   (WINAPI *JLINK_EraseChip)(void);
extern int   (WINAPI *JLINK_DownloadFile)(LPCSTR file, DWORD addr);
extern void  (WINAPI *JLINK_BeginDownload)(int index);
extern void  (WINAPI *JLINK_EndDownload)(void);

BOOL JLINK_Init(void);
